2020-2021 Sunseeker Telemetry and Lighting System
mpu_ex1_threeSeg.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 // MSP430FR59x Demo - MPU Write protection violation - Interrupt notification
34 //
35 // Description: The MPU segment boundaries are defined by:
36 // Border 1 = 0x6000 [MPUSEGB1 = 0x0600]
37 // Border 2 = 0x8000 [MPUSEGB2 = 0x0800]
38 // Segment 1 = 0x4400 - 0x5FFF
39 // Segment 2 = 0x6000 - 0x7FFF
40 // Segment 3 = 0x8000 - 0x13FFF
41 // Segment 2 is write protected. Any write to an address in the segment 2 range
42 // causes a PUC. The LED toggles after accessing SYS NMI ISR.
43 //
44 // ACLK = n/a, MCLK = SMCLK = default DCO
45 //
46 //
47 // MSP430FR5969
48 // ---------------
49 // /|\| |
50 // | | |
51 // --|RST |
52 // | |
53 // | P1.0|-->LED
54 //
55 //******************************************************************************
56 
57 #include "driverlib.h"
58 
59 uint8_t SYSNMIflag = 0;
60 uint16_t *ptr = 0;
61 uint16_t Data =0;
62 
63 void main(void)
64 {
65  // Stop WDT
66  WDT_A_hold(WDT_A_BASE);
67 
68  // Configure P1.0 for LED
69  GPIO_setAsOutputPin(
70  GPIO_PORT_P1,
71  GPIO_PIN0);
72 
73  /*
74  * Disable the GPIO power-on default high-impedance mode to activate
75  * previously configured port settings
76  */
77  PMM_unlockLPM5();
78 
79  // Configure MPU
80  MPU_initThreeSegmentsParam param = {0};
81  param.seg1boundary = 0x0600;
82  param.seg2boundary = 0x0800;
83  param.seg1accmask = MPU_READ|MPU_WRITE|MPU_EXEC;
84  param.seg2accmask = MPU_READ|MPU_EXEC;
85  param.seg3accmask = MPU_READ|MPU_WRITE|MPU_EXEC;
86  MPU_initThreeSegments(MPU_BASE, &param);
87 
88  MPU_disablePUCOnViolation(MPU_BASE,MPU_SECOND_SEG);
89 
90  MPU_enableNMIevent(MPU_BASE);
91  MPU_start(MPU_BASE);
92 
93  Data = 0x88;
94 
95  // Cause an MPU violation by writing to segment 2+
96  ptr = (uint16_t *)0x6002;
97  *ptr = Data;
98 
99  __delay_cycles(100);
100 
101  while(SYSNMIflag)
102  {
103  GPIO_toggleOutputOnPin(
104  GPIO_PORT_P1,
105  GPIO_PIN0
106  );
107  __delay_cycles(100000);
108  }
109 
110  // No violation - trap here
111  while(1);
112 }
113 
114 // Sys NMI vector
115 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
116 #pragma vector=SYSNMI_VECTOR
117 __interrupt
118 #elif defined(__GNUC__)
119 __attribute__((interrupt(SYSNMI_VECTOR)))
120 #endif
121 void SYSNMI_ISR(void)
122 {
123  switch(__even_in_range(SYSSNIV,0x18))
124  {
125  case 0x00: break;
126  case 0x02: break;
127  case 0x04: break;
128  case 0x06: break;
129  case 0x08: break;
130  case 0x0A: break;
131  case 0x0C: break; //MPUSEG1IFG
132  case 0x0E: //MPUSEG2IFG
133  // Clear violation interrupt flag
134  SYSNMIflag = 1; // Set flag
135  break;
136  case 0x10: break; // MPUSEG3IFG
137  case 0x12: break;
138  case 0x14: break;
139  case 0x16: break;
140  case 0x18: break;
141  default: break;
142  }
143 }
void main(void)
uint16_t * ptr
uint16_t Data
MPU_initThreeSegmentsParam param
MPU_start(MPU_BASE)
MPU_initThreeSegments(MPU_BASE, &param)
uint8_t SYSNMIflag
void SYSNMI_ISR(void)
MPU_disablePUCOnViolation(MPU_BASE, MPU_SECOND_SEG)
MPU_enableNMIevent(MPU_BASE)
__delay_cycles(500000)